/**
 * 
 */
init();

function init(){
	$.ajax({
		type:'post',
		url:'http://localhost:8088/myOA/departinfo',
		data:{
			"typeName":"DepartInfo",
			"method":"getPageDatas"
		},
		success:function(data){
			var trs="";
			var json=JSON.parse(data);
//			console.log(json);
			for(var i=0;i<json.length;i++){				
				trs+="<tr><td hidden>"+json[i].departId+"</td>";				
				trs+="<td>"+json[i].departName+"</td>";				
				trs+="<td>"+json[i].branchId+"</td>";
				trs+="<td>"+json[i].principalUser+"</td>";
				trs+="<td>"+json[i].connectTelNo+"</td>";
				trs+="<td>"+json[i].connectMobileTelNo+"</td>";
				trs+="<td>"+json[i].faxes+"</td>";
				trs+="<td><button onclick='modify(this)'>修改</button></td>";
				trs+="<td><button onclick='remove(this)'>删除</button></td></tr>";	
			}
			$("#division tbody").empty().append(trs);
		},
		error:function(res){
			console.log(res);
		}
	});
}

//展示添加窗口
$("#addDept").click(function(){
	//加载机构信息到下拉框
	getDepts();
	$("#addDeptForm").css({
		"top": "10vh",
		"transition": "top .3s ease",
	});
	$("#cover").css("display", "block").attr("onclick", "coverBack('#addDeptForm')");
});

//执行添加
$("#updateDept").click(function(){
	var ipts=$(".branchInfo");
	var depts={
		"departName":ipts.eq(0).val(),
		"branchId":ipts.eq(1).val(),
		"principalUser":ipts.eq(2).val(),
		"connectTelNo":ipts.eq(3).val(),
		"connectMobileTelNo":ipts.eq(4).val(),
		"faxes":ipts.eq(5).val(),				
	}
	$.ajax({
		type:'post',
		url:'http://localhost:8088/myOA/departinfo',
		data:{
			"typeName":"DepartInfo",
			"method":"add",
			"data":JSON.stringify(depts)
		},
		success:function(data){
			if(data!="true"){
				alert("添加失败");
			}else{
				alert("添加成功");
			}
			init();
			coverBack($("#addDeptForm"));
		},
		error:function(res){
			console.log("失败");
		}
	});
	
});

//弹出修改窗体
var deptInfo=[];
var id=0;
function modify(btn){
	deptInfo.length=0;
	var tds=$(btn).parents('tr').eq(0).children();
	id=tds.eq(0).text();
	for(var i=1;i<tds.length-2;i++){
		deptInfo.push(tds.eq(i).text());
	}
//	console.log(deptInfo);
	var deptInfos=$(".deptInfo");
//	console.log(deptInfos);
	for(var i=0;i<deptInfos.length;i++){		
		deptInfos[i].value=deptInfo[i];
	}	

	$("#modifyDeptForm").css({
		"top": "10vh",
		"transition": "top .5s ease",
	});
	$("#cover").css("display", "block").attr("onclick", "coverBack('#modifyDeptForm')");
}
//修改
$("#updateDeptConfirm").click(function(){
	var deptInfo=$(".deptInfo");
	var depts={
			"departId":id,
			"departName":deptInfo.eq(0).val(),
			"branchId":deptInfo.eq(1).val(),
			"principalUser":deptInfo.eq(2).val(),
			"connectTelNo":deptInfo.eq(3).val(),
			"connectMobileTelNo":deptInfo.eq(4).val(),
			"faxes":deptInfo.eq(5).val()
	}
//	console.log(depts);
	$.ajax({
		type:'post',
		url:'http://localhost:8088/myOA/departinfo',
		data:{
			"typeName":"DepartInfo",
			"method":"update",
			"data":JSON.stringify(depts)
		},
		success:function(data){
			if(data!="true"){
				alert("修改失败");
			}else{
				alert("修改成功");
			}
			init();
			coverBack($("#modifyDeptForm"));
		},
		error:function(res){
			console.log("网络错误");
		}
	});
});


//删除
function remove(btn){
	if(confirm("是否删除?")){
		var id=$(btn).parents('tr').eq(0).children().eq(0).text();
		var data={
				"departId":id
		}
		$.ajax({
			type:'post',
			url:'http://localhost:8088/myOA/departinfo',
			data:{
				"typeName":"DepartInfo",
				"method":"delete",
				"data":JSON.stringify(data)
			},
			success:function(data){
				if(data!="true"){
					alert("删除失败");
				}else{
					alert("删除成功");
				}
				init();	
			},
			error:function(res){
				console.log("网络错误");
			}
		});
	}
}
//点击关闭添加弹窗
$("#cancelAddDept").click(function(){
	coverBack($("#addDeptForm"));
})
//点击关闭修改弹窗
$("#cancelUpdateDept").click(function(){
	coverBack($("#modifyDeptForm"));
})